package cn.com.firmlink.cps
{
	import cn.com.firmlink.cps.common.navigation.ViewMoudle;
	
	import flexmdi.containers.MDICanvas;
	
	import mx.containers.ViewStack;
	import mx.core.Container;

	public class MainViewStack
	{
		public function MainViewStack()
		{
		}

		private var _vs:ViewStack;
		
		public var mdiCanvas:MDICanvas;

		public var currentView:Container;

		public function get viewStack():ViewStack
		{
			return this._vs;
		}

		public function set viewStack(vs:ViewStack):void
		{
			this._vs=vs;
		}

		private static var instance:MainViewStack=null;

		public static function getInstance():MainViewStack
		{
			if (instance == null)
			{
				instance=new MainViewStack();
			}
			return instance;
		}

		public function changeView(viewId:String, defaultTitle:String="", addActionBar:Boolean=true, loadData=true):void
		{
			currentView=viewStack.getChildByName(viewId) as Container;
			if (currentView == null || currentView instanceof DefaultView)
			{
				currentView=changeToDefaultView(defaultTitle);
			}
			viewStack.selectedChild=currentView;

			if (addActionBar)
			{
				var title:String=viewId;
				if (currentView is ModuleTitle)
				{
					title=ModuleTitle(currentView).title;
				}
				var viewMoudle:ViewMoudle=new ViewMoudle(viewId, title);
				GlobalModel.getInstance().actionQueue.put(viewMoudle);
			}
		}

		public function changeToDefaultView(defaultTitle:String=""):Container
		{
			currentView=viewStack.getChildByName("DefaultView") as Container;
			var defaultView:DefaultView=DefaultView(currentView);
			defaultView.title=defaultTitle;
			clearView();

			return defaultView;
		}

		public function clearView():void
		{
			viewStack.selectedChild=viewStack.getChildByName("ClearView") as Container;
		}
	}
}